<?php


namespace app\controller\equipment;
require_once __DIR__.'/../../common/AuthController.php';
use app\common\AuthController;


/**
 * 订单管理
 */
class Order extends AuthController
{   


    /**
     * 初始化操作
     */
    public function init()
    {

    }
	

    /**
     * 获取订单列表
     */
	public function api_equipment_order_getList($msg)
    {   
        global $db;
        $info = [
            'page' =>  isset($msg['data']['page'])?$msg['data']['page']:1,
            'rows' => isset($msg['data']['rows'])?$msg['data']['rows']:15,
            'id' => isset($msg['data']['id'])?$msg['data']['id']:'',
            'order_no' => isset($msg['data']['order_no'])?$msg['data']['order_no']:'',
            'out_trade_no' => isset($msg['data']['out_trade_no'])?$msg['data']['out_trade_no']:'',
            'equipment_name' => isset($msg['data']['equipment_name'])?$msg['data']['equipment_name']:'',
            'merchant_name' => isset($msg['data']['merchant_name'])?$msg['data']['merchant_name']:'',
            'itoc_name' => isset($msg['data']['itoc_name'])?$msg['data']['itoc_name']:'',
            'region_name' => isset($msg['data']['region_name'])?$msg['data']['region_name']:'',
            'pay_type' => isset($msg['data']['pay_type'])?$msg['data']['pay_type']:'',
            'order_status' => isset($msg['data']['order_status'])?$msg['data']['order_status']:'',
        ];
        $limit = ($info['page']-1)*$info['rows'].','.$info['rows'];
        $where = "is_delete = 0";
        if(!empty($info['id'])){
            $where .= " AND id =".$info['id'];
        }
        if(!empty($info['pay_type'])){
            $where .= " AND pay_type =".$info['pay_type'];
        }
        if(!empty($info['order_status'])){
            $where .= " AND order_status =".$info['order_status'];
        }
        if(!empty($info['order_no'])){
            $where .= " AND order_no LIKE '%".$info['order_no']."%'";
        }
        if(!empty($info['out_trade_no'])){
            $where .= " AND out_trade_no LIKE '%".$info['out_trade_no']."%'";
        }
        if(!empty($info['equipment_name'])){
            $where .= " AND equipment_name LIKE '%".$info['equipment_name']."%'";
        }
        if(!empty($info['merchant_name'])){
            $where .= " AND merchant_name LIKE '%".$info['merchant_name']."%'";
        }
        if(!empty($info['region_name'])){
            $where .= " AND region_name LIKE '%".$info['region_name']."%'";
        }
        if(!empty($info['itoc_name'])){
            $where .= " AND itoc_name LIKE '%".$info['itoc_name']."%'";
        }
		$list = $db->query("select * from `order` where ".$where ." order by id  desc limit ".$limit); 
        $count =  $db->query("SELECT count(*) as count from `order` WHERE ".$where);
        if(!empty($list)){
            foreach ($list as $k => $v) {
                // $list[$k]['goods_list'] = $db->query("select * from order_goods where order_no = '".$v['order_no']."'"); 
                $order_goods = $this->table->find('order_goods',['order_no'=>$v['order_no']]);
                $equipment = $this->table->find('equipment_list',['id'=>$v['equipment_id']]);
                $goods = $this->table->find('goods',['id'=>$order_goods['goods_id']]);
                $list[$k]['goods_id'] = $order_goods['goods_id'];
                $list[$k]['goods_code'] = $order_goods['goods_code'];
                $list[$k]['goods_name'] = $order_goods['goods_name'];
                $list[$k]['goods_pic'] = $order_goods['goods_pic'];
                $list[$k]['attribute'] = $order_goods['attribute'];
                $list[$k]['number'] = $order_goods['number'];
                $list[$k]['price'] = $order_goods['price'];
                $list[$k]['merchant_circle_name'] = $equipment['merchant_circle_name'];
                $list[$k]['equipment_code'] = $equipment['code'];
                $list[$k]['category_name'] = $equipment['category_name'];
                $list[$k]['brand_name'] = $goods['brand_name'];
                $list[$k]['units_name'] = $goods['units_name'];
                $list[$k]['category_name_2'] = $goods['category_name_2'];
            }
        }
        return $this->outMessage(10000,'获取成功',$list,'success',$count[0]['count']);
	}




    /**
     * 终端销售量统计
     * @param date 1=月，2=周，3=季度，4=年
     * @param type 1=终端销售额统计
     */
    public function api_equipment_order_salesStat($msg)
    {
        global $db;
        $info = [
            'page' => isset($msg['data']['page'])?$msg['data']['page']:1,
            'rows' => isset($msg['data']['rows'])?$msg['data']['rows']:15,
            'date' => isset($msg['data']['date'])?$msg['data']['date']:1,
            'category_id' => isset($msg['data']['category_id'])?$msg['data']['category_id']:'',
            'merchant_circle_id' => isset($msg['data']['merchant_circle_id'])?$msg['data']['merchant_circle_id']:'',
            'itoc_id' => isset($msg['data']['itoc_id'])?$msg['data']['itoc_id']:'',
            'admin_region_id' => isset($msg['data']['admin_region_id'])?$msg['data']['admin_region_id']:'',
            'type' => isset($msg['data']['type'])?$msg['data']['type']:'',
        ];
        if(empty($info['date']) || !is_numeric($info['date']) || $info['date'] < 1 || $info['date'] > 4){
            return $this->outMessage(-1,'时间筛选错误');
        }
        $limit = ($info['page']-1)*$info['rows'].','.$info['rows'];
        $where = " a.order_status = 2";
        if(!empty($info['category_id'])){
            $where .= " AND b.category_id in (".$info['category_id'].") ";
        }
        if(!empty($info['merchant_circle_id'])){
            $where .= " AND b.merchant_circle_id in (".$info['merchant_circle_id'].") ";
        }
        if(!empty($info['itoc_id'])){
            $where .= " AND a.itoc_id =".$info['itoc_id'];
        }
        //如果存在大区筛选
        if(!empty($info['admin_region_id'])){
            $region_id = $db->query("SELECT region_ids FROM admin_region WHERE id in (".$info['admin_region_id'].")");
            $ids = '';
            foreach ($region_id as $k => $v) {
                if(empty($ids)){
                    $ids .= $v['region_ids'];
                }else{
                    $ids .= ','.$v['region_ids'];
                }
            }
            $region_ids = $this->table->getRegionIds($ids);
            if($region_ids != 'all' && !empty($region_ids)){
                $where .= " AND a.region_id in (".$region_ids.") ";
            }
        }
        $time = $this->common->getTime($info['date'],'',7);
        $where .= " AND a.create_time > '".$time['start'][5]."'"; 
        $list = $db->query("SELECT b.code,a.itoc_name,a.equipment_id FROM `order` as a left join equipment_list as b on a.equipment_id = b.id WHERE ".$where." GROUP BY a.equipment_id limit ".$limit);
        $field = "number";
        if($info['type'] == 1){
            $field = 'sum_price';
        }
        if(!empty($list)){
            foreach ($list as $k => $v) {       
                for($i=0;$i<7;$i++){
                    $data = $db->query("SELECT SUM($field) as count FROM `order` as a LEFT JOIN order_goods as b on a.order_no = b.order_no WHERE a.equipment_id = ".$v['equipment_id']." AND a.create_time > '".$time['start'][$i]."' AND a.create_time < '".$time['stop'][$i]."'");
                    $list[$k][$i] = !empty($data[0]['count'])?$data[0]['count']:0;
                }
                $list[$k]['date'] = $info['date'];  
            }
        }
        $count = $db->query("SELECT count(*) as count FROM `order` as a left join equipment_list as b on a.equipment_id = b.id WHERE ".$where." GROUP BY a.equipment_id");
        return $this->outMessage(10000,'获取成功',$list,'success',$count[0]['count']);  
    }



    /**
     * 终端销售量统计详情
     * @param date 1=月，2=周，3=季度，4=年
     * @param type 1=终端销售额统计
     */
    public function api_equipment_order_salesStatDetails($msg)
    {   
        global $db;
        $info = [
           'page' => isset($msg['data']['page'])?$msg['data']['page']:1,
           'rows' => isset($msg['data']['rows'])?$msg['data']['rows']:15, 
           'equipment_id' => isset($msg['data']['equipment_id'])?$msg['data']['equipment_id']:'',
           'date' => isset($msg['data']['date'])?$msg['data']['date']:1,
           'number' => isset($msg['data']['number'])?$msg['data']['number']:'',
           'type' => isset($msg['data']['type'])?$msg['data']['type']:'',
        ];
        if(!$info['equipment_id'] || !$info['number']){
            return $this->outMessage(-1,'参数不能为空');
        }
        $limit = ($info['page']-1)*$info['rows'].','.$info['rows'];
        $time = $this->common->getTime($info['date'],$info['number']);
        $field = '';
        if($info['type'] == 1){
            $field = ",SUM(sum_price) as sum_price";
        }
        $list = $db->query("SELECT a.goods_id,a.goods_name,c.attribute,c.brand_name,SUM(a.number) as number".$field." FROM order_goods as a left join `order` as b on a.order_no = b.order_no left join goods as c on a.goods_id = c.id WHERE b.order_status = 2 AND b.equipment_id = ".$info['equipment_id']." AND b.create_time > '".$time['start']."' AND b.create_time < '".$time['stop']."' GROUP BY a.goods_id limit ".$limit);
        $count = $db->query("SELECT count(*) as count FROM order_goods as a left join `order` as b on a.order_no = b.order_no left join goods as c on a.goods_id = c.id WHERE b.order_status = 2 AND b.equipment_id = ".$info['equipment_id']." AND b.create_time > '".$time['start']."' AND b.create_time < '".$time['stop']."' GROUP BY a.goods_id");
        return $this->outMessage(10000,'获取成功',$list,'success',$count[0]['count']);  
    }



    /**
     * 支付方式统计
     */
    public function api_equipment_order_payTypeStat($msg)
    {
        global $db;
        $date = isset($msg['data']['date'])?$msg['data']['date']:1;
        if(empty($date) || !is_numeric($date) || $date < 1 || $date > 4){
            return $this->outMessage(-1,'时间筛选错误');
        }
        $where = " order_status = 2";
        $time = $this->common->getTime($date,'',7);
        $where .= " AND create_time > '".$time['start'][5]."'"; 
        $list = $db->query("SELECT pay_type FROM `order` WHERE ".$where." GROUP BY pay_type");
        if(!empty($list)){
            foreach ($list as $k => $v) {       
                for($i=0;$i<7;$i++){
                    $data = $db->query("SELECT count(*) as count FROM `order` WHERE pay_type = ".$v['pay_type']." AND create_time > '".$time['start'][$i]."' AND create_time < '".$time['stop'][$i]."'");
                    $list[$k][$i] = !empty($data[0]['count'])?$data[0]['count']:0;
                }
                $list[$k]['date'] = $date;  
            }
        }
        return $this->outMessage(10000,'获取成功',$list);  
    }


 

}
